<?php
class ModelWxWx extends Model {
    public function addUser($data) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "wechat_user SET openid='".$data['openid']."',nickname='".$data['nickname']."',sex='".$data['sex']."',province='".$data['province']."',city='".$data['city']."',country='".$data['country']."',headimgurl='".$data['headimgurl']."',subscribe=0,share_status=0,referee_id='',date_added=NOW(),lottery_times =1");
        return true;
    }
    public function getRefereeTotal($openid){
        $query=$this->db->query("SELECT count(openid) as total FROM " . DB_PREFIX . "wechat_user WHERE referee_id='".$openid."' AND date_added=to_days(now())");
        return $query->row['total'];
    }
    public function getuserinfo($openid) {
        $query=$this->db->query("SELECT * FROM " . DB_PREFIX . "wechat_user WHERE openid='".$openid."'");
        return $query->row;
    }

    public function getlotterytimes($openid){
        $query=$this->db->query("SELECT lottery_times FROM " . DB_PREFIX . "wechat_user WHERE openid='".$openid."'");
        return $query->row['lottery_times'];
    }
    public function subtractLotteryTimes($openid){
        $query=$this->db->query("UPDATE " . DB_PREFIX . "wechat_user SET lottery_times = lottery_times-1 WHERE openid='".$openid."'");
        return true;
    }
    public function addLotteryTimes($openid){
        $query=$this->db->query("UPDATE " . DB_PREFIX . "wechat_user SET lottery_times = lottery_times+1 WHERE openid='".$openid."'");
        return true;
    }
    public function share($openid) {
        $this->db->query("UPDATE " . DB_PREFIX . "wechat_user SET share_status = 1 WHERE openid='".$openid."'");
        return true;
    }
    public function addUserprize($openid,$prize) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "wechat_user_prize SET openid = '$openid',prize='".$prize."',datetime=NOW()");
        return true;
    }
    public function updateUserSubscribeStatus($openid,$status) {
        $this->db->query("UPDATE " . DB_PREFIX . "wechat_user SET subscribe = '".$status."' WHERE openid='".$openid."'");
        return true;
    }
    public function updateUserRefereeid($openid,$referee_id) {
        $this->db->query("UPDATE " . DB_PREFIX . "wechat_user SET referee_id = '".$referee_id."' WHERE openid='".$openid."'");
        return true;
    }
    public function updateWinnerInfo($openid,$data) {
        $this->db->query("UPDATE " . DB_PREFIX . "wechat_user SET name = '".$data['name']."',telephone = '".$data['telephone']."' WHERE openid='".$openid."'");
        return true;
    }

}
?>